package trading.ctm.yunjing.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;

import trading.ctm.yunjing.R;


/**
 * 自定义简单曲线图
 * <p/>
 * Created by CQ
 */
public class LineGraphicViewY extends View {
    /**
     * 公共部分
     */
    private Context mContext;
    private Paint mPaint;
    private Paint mPaint2;
    private Resources res;
    private DisplayMetrics dm;
    private int canvasHeight;
    private int bheight = 0;
    private int blwidh;
    private boolean isMeasure = true;
    /**
     * Y轴最大值
     */
    private int maxValue;
    /**
     * Y轴间距值
     */
    private int averageValue;
    private int marginTop = 20;
    private int marginBottom = 40;

    /**
     * 纵坐标值
     */
    private ArrayList<Double> yRawData;
    /**
     * 横坐标值
     */
    private int spacingHeight = 5;

    public LineGraphicViewY(Context context) {
        this(context, null);
    }

    public LineGraphicViewY(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }


    private void initView() {
        this.res = mContext.getResources();
        this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this.mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = wm.getDefaultDisplay();
        defaultDisplay.getMetrics(dm);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (isMeasure) {
            this.canvasHeight = getHeight();
            if (bheight == 0)
                bheight = canvasHeight - marginBottom - 100;
            blwidh = dip2px(25);
            isMeasure = false;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(res.getColor(R.color.color_1A693C26));
        mPaint2.setColor(res.getColor(R.color.color_FFFFFF));
        drawAllXLine(canvas);
    }


    /**
     * 画所有横向表格，包括X轴
     */
    private void drawAllXLine(Canvas canvas) {
        for (int i = 0; i < spacingHeight + 1; i++) {

            if (i == spacingHeight) {
                drawTextY("元/kg",
                        blwidh / 2 - 10,
                        bheight - (bheight / spacingHeight) * i + marginTop + 5,
                        canvas);
                break;
            }

            drawTextY(
                    String.valueOf(averageValue * i),
                    blwidh / 2 - 10,
                    bheight - (bheight / spacingHeight) * i + marginTop + 5,
                    canvas);
        }
    }


    private void drawTextY(String text, int x, int y, Canvas canvas) {
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setTextSize(dip2px(10));
        p.setColor(res.getColor(R.color.color_9C9C9C));
        p.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text, x + 10, y + dip2px(5), p);
    }


    public void setData(ArrayList<Double> yRawData,
                        int maxValue, int averageValue) {
        this.yRawData = yRawData;
        this.maxValue = maxValue;
        this.averageValue = averageValue;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dip2px(float dpValue) {
        return (int) (dpValue * dm.density + 0.5f);
    }


}
